package com.jyx.order.controller;

import com.jyx.api.order.OrderControllerApi;
import com.jyx.framework.domain.order.request.CreateOrderRequest;
import com.jyx.framework.domain.order.request.PayQrcodeRequest;
import com.jyx.framework.domain.order.response.CreateOrderResult;
import com.jyx.framework.domain.order.response.OrderResult;
import com.jyx.framework.domain.order.response.PayOrderResult;
import com.jyx.framework.domain.order.response.PayQrcodeResult;
import com.jyx.framework.exception.ExceptionCast;
import com.jyx.framework.model.response.CommonCode;
import com.jyx.framework.utils.JyxOauth2Util;
import com.jyx.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/order")
public class OrderController implements OrderControllerApi {

    @Autowired
    OrderService orderService;

    @Override
    @PostMapping("/create")
    public CreateOrderResult createOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest request) {
        JyxOauth2Util jyxOauth2Util = new JyxOauth2Util();
        JyxOauth2Util.UserJwt userJwt = jyxOauth2Util.getUserJwtFromHeader(request);
        if(userJwt == null){
            ExceptionCast.cast(CommonCode.UNAUTHENTICATED);
        }
        String courseId = createOrderRequest.getCourseId();
        String userId = userJwt.getId();
        return orderService.createOrder(courseId,userId);
    }

    @Override
    @GetMapping("/get/{orderNum}")
    public OrderResult queryOrderByNum(@PathVariable("orderNum") String orderNum, HttpServletRequest request) {
        /*JyxOauth2Util jyxOauth2Util = new JyxOauth2Util();
        JyxOauth2Util.UserJwt userJwt = jyxOauth2Util.getUserJwtFromHeader(request);
        if(userJwt == null){
            ExceptionCast.cast(CommonCode.UNAUTHENTICATED);
        }*/
        return orderService.getOrderByNum(orderNum);
    }

    @Override
    @PostMapping("/pay/createWeixinQrcode")
    public PayQrcodeResult createPayQrcode(@RequestParam String orderNumber) {
        return orderService.createPayQrcode(orderNumber);
    }

    @Override
    @GetMapping("/pay/queryWeixinPayStatus")
    public PayOrderResult queryPayResult(@RequestParam String orderNumber) {

        return orderService.queryPayResult(orderNumber);
    }


}
